<?php

namespace beObject\Primitive;

use beObject\Object;
use beObject\Exceptions\IllegalArgumentException;

/**
 * Object used to represent a float.
 * @author Dany Mottier
 * @copyright MD Consult
 * @license http://www.gnu.org/licenses/gpl.html
 * @link http://www.beobject-framework.net/beobject/primary/char/float-class/<br />
 * @package beObject\Primitive
 * @version 3.0
 */
class Float
    extends Object {

  /** @var float The real float to work on. */
  private $float = null;

  /**
   * Instantiate a new float object with the given float value.
   * @param float $value The inner private property float value.
   * @throws beObject\Primitive\IllegalArgumentException if the given parameter
   * is not a float.
   * @link http://www.beobject-framework.net/beobject/primary/float/float-class/<br />
   */
  public function __construct($value) {
    if (is_float($value)) {
      $this->float = $value;
    } else {
      throw new IllegalArgumentException("\$value parameter must be a float.");
    }
  }

  /**
   * Returns the inner private property when the object is called like a method.
   * @return float The inner private property value.
   * @link http://www.beobject-framework.net/beobject/primary/float/float-__invoke/
   */
  public function __invoke() {
    return $this->float;
  }

  /**
   * Parse any object to a float.
   * @param mixed $value The object to parse to a float.
   * @return beObject\Primitive\Float The parsed float value.
   * @link http://www.beobject-framework.net/beobject/primary/float/float-parsefloat/<br />
   * @throws beObject\Exceptions\IllegalArgumentException if the given variable
   * can not be parsed to a float.
   */
  public static function parseFloat($value) {
    $float = null;

    switch (gettype($value)) {
      case "integer" :
      case "boolean" :
      case "string" :
        $float = new Float(floatval($value));
        break;
      case "object" :
        switch (get_class($value)) {
          case "beObject\Primitive\String" :
            $float = new Float(floatval($value->toString()));
            break;
          case "beObject\Primitive\Boolean" :
            $float = new Float(floatval($value->booleanValue()));
            break;
          case "beObject\Primitive\Integer" :
            $float = new Float(floatval($value->integerValue()));
            break;
        }
    }

    if (!is_null($float)) {
      return $float;
    } else {
      throw new IllegalArgumentException("Can not parse given parameter to a float.");
    }
  }

  /**
   * Returns the inner private property value.
   * @return string The string representation of the float value.
   * @link http://www.beobject-framework.net/beobject/primary/float/float-tostring/
   */
  public function toString() {
    return strval($this->float);
  }

  /**
   * Returns a string that represent the float value.
   * @return string The string representation of the float value.
   * @link http://www.beobject-framework.net/beobject/primary/float/float-__tostring/
   */
  public function __toString() {
    return strval($this->float);
  }

  /**
   * Returns the inner private property value.
   * @return float The real float value.
   * @link http://www.beobject-framework.net/beobject/primary/float/float-floatvalue/
   */
  public final function floatValue() {
    return $this->float;
  }

}

?>
